using HtERP.Data;

namespace HtERP.Services;

/// <summary>
/// 数据库备份服务接口
/// </summary>
public interface IDbBackupService
{
    /// <summary>
    /// 执行数据库备份
    /// </summary>
    /// <param name="databaseType">数据库类型</param>
    /// <param name="connectionString">数据库连接字符串</param>
    /// <param name="backupPath">备份文件保存路径</param>
    /// <param name="compress">是否压缩</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>备份结果</returns>
    Task<BackupResult> BackupDatabaseAsync(string databaseType, string connectionString, string backupPath, bool compress = true, CancellationToken cancellationToken = default);
    
    /// <summary>
    /// 测试数据库连接
    /// </summary>
    /// <param name="databaseType">数据库类型</param>
    /// <param name="connectionString">数据库连接字符串</param>
    /// <returns>连接是否成功</returns>
    Task<bool> TestConnectionAsync(string databaseType, string connectionString);
    
    /// <summary>
    /// 获取支持的数据库类型
    /// </summary>
    /// <returns>数据库类型列表</returns>
    List<string> GetSupportedDatabaseTypes();
    
    /// <summary>
    /// 检查磁盘空间是否充足
    /// </summary>
    /// <param name="directoryPath">目录路径</param>
    /// <param name="thresholdMB">阈值（MB）</param>
    /// <returns>磁盘空间是否不足</returns>
    bool CheckDiskSpace(string directoryPath, int thresholdMB);

    /// <summary>
    /// 清理旧的备份文件
    /// </summary>
    /// <param name="schedule">备份计划</param>
    Task CleanupOldBackupsAsync(BackupSchedule schedule);

    /// <summary>
    /// 创建备份目录
    /// </summary>
    /// <param name="path">目录路径</param>
    void CreateBackupDirectory(string path);

    /// <summary>
    /// 生成默认的备份文件路径
    /// </summary>
    /// <param name="databaseType">数据库类型</param>
    /// <param name="databaseName">数据库名称</param>
    /// <param name="basePath">基础路径</param>
    /// <returns>生成的备份文件路径</returns>
    string GenerateBackupFilePath(string databaseType, string databaseName, string basePath);
}

/// <summary>
/// 备份结果类
/// </summary>
public class BackupResult
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }
    
    /// <summary>
    /// 备份文件路径
    /// </summary>
    public string BackupFilePath { get; set; } = string.Empty;
    
    /// <summary>
    /// 备份文件大小（字节）
    /// </summary>
    public long FileSize { get; set; }
    
    /// <summary>
    /// 错误信息
    /// </summary>
    public string ErrorMessage { get; set; } = string.Empty;
    
    /// <summary>
    /// 显示消息
    /// </summary>
    public string Message { get; set; } = string.Empty;
    
    /// <summary>
    /// 备份耗时（毫秒）
    /// </summary>
    public long DurationMs { get; set; }
}